//
//  BaseVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import MBProgressHUD

class BaseVC: UIViewController {
  
  enum BackItemColor {
    case white
    case blue
  }
  
    fileprivate lazy var backItemBtn: UIButton = {
        let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        button.setImage(UIImage(named: "nav-return-icon"), for: .normal)
        button.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        button.contentHorizontalAlignment = .left
        button.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
        button.titleLabel?.font = GlobalConfig.system16Font
        return button
    }()
  override func viewDidLoad() {
    super.viewDidLoad()
    whiteNavigationBackgroud()
    automaticallyAdjustsScrollViewInsets = false
  }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        #if DEBUG
             print("class = \(classForCoder)","action = viewWillAppear")
        #endif
       
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        #if DEBUG
             print("class = \(classForCoder)","action = viewWillDisappear")
        #endif
       
    }
    
    
}

// MARK: - action

extension BaseVC {
  @objc fileprivate func backAction() {
    let _ = navigationController?.popViewController(animated: true)
  }
}

extension BaseVC {

    /// 用来给子控制器覆盖,用来处理没有数据时的占位视图
    func placeholderViewLayout() {
        
    }
    
}

// MARK: - NavigationController

extension BaseVC {
  
    func blueNavigationBackgroud() {
        
        navigationController?.navigationBar.titleTextAttributes = GlobalConfig.NavBarRootTitleAttributes
        navigationController?.navigationBar.setBackgroundImage(GlobalConfig.BGSkyBlueImage, for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        
        
        
    }
    func whiteNavigationBackgroud() {
        navigationController?.navigationBar.setBackgroundImage(GlobalConfig.BGWhiteImage, for: .default)
        navigationController?.navigationBar.titleTextAttributes = GlobalConfig.NavBarStackTitleAttributes
        navigationController?.navigationBar.shadowImage = UIImage()
    }
  func deleteBackItem() {
    self.backItemBtn.setImage(UIImage(), for: .normal)
    self.backItemBtn.setTitle("", for: .normal)
  }
  
  func addBackTitleItem(title: String) {
    self.backItemBtn.setImage(UIImage(), for: .normal)
    self.backItemBtn.setTitle(title, for: .normal)
  }
  
  func addBackItem(color: BackItemColor = .blue) {
    var image: UIImage = UIImage()
    switch color {
    case .blue:
        image = UIImage(named: "nav-return-icon")!
        break
    case .white:
        image = UIImage(named: "nav-return-white-icon")!
        break
    }
    self.backItemBtn.setImage(image, for: .normal)
  }

    override func customBackItem(withTarget target: Any!, action: Selector!) -> UIBarButtonItem! {
        return UIBarButtonItem.init(customView: backItemBtn)
    }
}
